package com.dgut.service.background.service;

import com.dgut.service.background.entity.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service
public class AdminDetailService implements UserDetailsService {
    @Autowired
    AdminService adminService;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        int adminId = Integer.parseInt(s);
        Admin admin = adminService.findById(adminId);
        if (admin != null){
            UserDetails details = User.withUsername(admin.getName()).password(admin.getPassword()).authorities(admin.getAuthority()).build();
             return details;
        }
        return null;
    }
}
